package com.wbpay.sdk.pay.webpay;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.wbpay.sdk.constant.Constants;
import com.wbpay.sdk.framework.IPayFunction;
import com.wbpay.sdk.framework.PayResult;
import com.wbpay.sdk.params.PayParams;
import com.wbpay.sdk.pay.IPay;
import com.wbpay.sdk.utils.Log;
import com.wbpay.sdk.utils.Utils;
import com.wbpay.sdk.wrapper.JubaoPayWrapper;
import com.wbpay.sdk.widget.JyouActivity;


/**
 * 易宝
 */
public class JubaoPay implements IPay {

    private Context mContext;
    private IPayFunction mPayFunction;

    @Override
    public void init(Context context, IPayFunction f) {
        mContext = context;
        mPayFunction = f;
    }

    @Override
    public void pay(PayParams payParams) {
        if (payParams == null) {
            String desc = mContext.getString(Utils.getStringId(mContext, "jy_null_payparams"));
            mPayFunction.notifyPayResult(PayResult.PAY_FAILURE, desc, this);
            return;
        }
        String broadcastAction = setupAndGetBroadcast();
        Intent intent = new Intent(mContext, JyouActivity.class);
        intent.putExtra(Constants.EXTRA_AWCN, JubaoPayWrapper.class.getName());
        intent.putExtra(Constants.EXTRA_CALLBACK, broadcastAction);
        intent.putExtra(Constants.EXTRA_FULLSCREEN, Utils.isFullScreen(mContext));

        intent.putExtra("server", payParams.getServer());
        intent.putExtra("price", Utils.convertFenToYuan(payParams.getPrice()));
        intent.putExtra("attach", payParams.getAttach());
        intent.putExtra("subject", payParams.getSubject());
        intent.putExtra("order", payParams.getOrder());
        intent.putExtra("body", payParams.getBody());
        try {
            mContext.startActivity(intent);
        } catch (Exception e) {
            if (mPayFunction != null) {
                String desc = mContext.getString(Utils.getStringId(mContext, "jy_unknown"));
                mPayFunction.notifyPayResult(PayResult.PAY_FAILURE, desc, this);
            }
        }
    }

    @Override
    public String getPayName() {
        return mContext.getString(Utils.getStringId(mContext, "jy_jubao_pay"));
    }

    @Override
    public int getPayType() {
        return PAY_TYPE_JUBAO_PAY;
    }

    @Override
    public boolean checkPayAvailable() {
        return false;
    }

    @Override
    public int getPayIcon() {
        return 0;
    }

    @Override
    public String getPayDesc() {
        return mContext.getString(Utils.getStringId(mContext, "jy_jubaopay_desc"));
    }

    @Override
    public String toString() {
        return getPayName();
    }

    /**
     * 设置易宝支付回调广播
     *
     * @return
     */
    private String setupAndGetBroadcast() {
        try {
            // 注销之前注册的广播
            mContext.unregisterReceiver(mBroadcastReceiver);
        } catch (Exception e) {
        }
        String action = mContext.getPackageName() + ".intent.action.JUBAOPAYRESULT";
        IntentFilter filter = new IntentFilter(action);
        mContext.registerReceiver(mBroadcastReceiver, filter);
        return action;
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mContext.unregisterReceiver(mBroadcastReceiver);
            int error = -1;
            String desc = null;
            if (intent != null) {
                error = intent.getIntExtra("RESULT_CODE", -1);
                desc = intent.getStringExtra("RESULT_DESC");
            }
            Log.d(Log.TAG, "error : " + error + " , desc : " + desc);
            processYeePayResult(error, desc);
        }
    };

    private void processYeePayResult(int error, String desc) {
        if (mPayFunction != null) {
            mPayFunction.notifyPayResult(error, desc, this);
        }
    }
}
